<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 订单管理
 */
namespace app\ais\controller\shop;
use app\ais\controller\Common;
use app\ais\model\AisShopOrder;
use think\facade\Request;
use think\helper\Time;

class Order extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'订单管理','url'=>url("ais/shopOrder/index")]]);
    }

    /**
     * 列表
     */
    public function index(int $types = 1){
        $condition = [];
        $condition[] = ['AisShopOrder.member_miniapp_id','=',$this->member_miniapp_id];
        $condition[] = ['AisShopOrder.is_del', '=',0];
        $time      = Request::param('time/d',0);
        $starttime = Request::param('starttime/s');
        $endtime   = Request::param('endtime/s');
        if ($time) {
            switch ($time) {
                case 2:
                    list($start, $end) = Time::yesterday();
                    break;
                case 30:
                    list($start, $end) = Time::month();
                    break;
                case 60:
                    list($start, $end) = Time::lastMonth();
                    break;
                default:
                    list($start, $end) = Time::today();
                    break;
            }
            $condition[] = ['create_time', '>=', $start];
            $condition[] = ['create_time', '<=', $end];
        } else {
            if ($starttime) {
                $condition[] = ['create_time', '>=', strtotime($starttime)];
            }
            if ($endtime) {
                $condition[] = ['create_time', '<=', strtotime($endtime)];
            }
        }
        switch ($types) {
            case 1:
                $condition[] = ['paid_at', '=',1];
                $condition[] = ['status', '=', 0];
                break;
            case 2:
                $condition[] = ['paid_at', '=',1];
                $condition[] = ['status', '=', 1];
                break;
            default:
                $condition[] = ['paid_at', '=',0];
                break;
        }
        $keyword  = Request::param('keyword/s');
        $lists = AisShopOrder::hasWhere('shop', function($query) use($keyword) {
           if($keyword){
               $query->where('phone|name', 'like', '%'.$keyword.'%', 'or');
           }
        })->where($condition)->order('id desc')->paginate(20, false, ['query' => ['keyword' => $keyword, 'starttime' => $starttime, 'endtime' => $endtime, 'time' => $time]]);
        $view['pay']       = AisShopOrder::where($this->mini_program)->where(['paid_at' => 1])->sum('amount');
        $view['invalid']   = AisShopOrder::where($this->mini_program)->where(['status' => 1])->sum('amount');
        $view['lists']     = $lists;
        $view['types']     = $types;
        $view['keyword']   = $keyword;
        $view['time']      = $time;
        $view['starttime'] = $starttime;
        $view['endtime']   = $endtime;
        return view()->assign($view);
    }
    
    /**
     * 订单详情
     */
    public function detail(int $id){
        $view['info'] = AisShopOrder::where(['id' => $id])->find();
        return view()->assign($view);
    }

    /**
     * 删除
     */
    public function delete($id){
        $result = AisShopOrder::update(['is_del'=>1],['id' => $id]);
        if($result){
            return enjson(200,'操作成功',['url' => url('ais/shopOrder/index',['status' => $this->request->param('status/d')])]);
        }
        return enjson(0,'操作失败');
    }

    /**
     * 订单完成
     */
    public function force_completion($id){
        $result = AisShopOrder::update(['status'=>1],['id' => $id]);
        if($result){
            return enjson(200,'操作成功',['url' => url('ais/shopOrder/index',['status' => $this->request->param('status/d')])]);
        }else{
            return enjson(0,'操作失败');
        }
    } 
}